package com.piz.web.product;

import org.springframework.stereotype.Component;
import org.springframework.validation.Errors;
import org.springframework.validation.Validator;

import com.piz.model.ItemDetails;
import com.piz.model.ItemPricing;
import com.piz.model.common.ItemCategoryEnum;


@Component("ProductValidation")
public class AddProductValidation implements Validator{
	
	
	public boolean supports(Class<?> klass) {
		return ItemDetails.class.isAssignableFrom(klass);
	}
	
	public void validate(Object target, Errors errors) 
	{
		ItemDetails itemDetails = (ItemDetails) target;
		
		if(!errors.hasErrors() && ((itemDetails.getCategoryId() == ItemCategoryEnum.PIZZA.getId())
				|| (itemDetails.getCategoryId() == ItemCategoryEnum.TOPPING.getId())))
		{
			int i = 1;
			
			for(ItemPricing itemPricing :  itemDetails.getPrices())
			{
				String base = "";
				switch (i) {
				case 1:
					base = "Small";
					break;
				case 2:
					base = "Medium";
					break;
				case 3:
					base = "Large";
					break;
				default:
					base = "";
					break;
				}
				if(itemPricing.getPrice() == null)
				{
					errors.rejectValue("prices["+i+"].price",
							"matchingPassword.registration.password",
							base+" Price is required");
				}
				i++;
			}
		}
		else
		{
			if(itemDetails.getSinglePrice() == null)
			{
				errors.rejectValue("singlePrice",
						"matchingPassword.registration.password",
						"Price is required");
			}
		}
	if((itemDetails.getCategoryId() == ItemCategoryEnum.PIZZA.getId()))
	{
		if(itemDetails.getFile().getSize()==0){
			   errors.rejectValue("file", "error.empty.file", "Please Select File.");
			}
		  if(itemDetails.getFile().getSize() > 30000){
			   errors.rejectValue("file", "error.empty.file", "File size more than 25 KB ");
			}
	}
		
	}

}
